/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 编辑器;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import 编辑器.tabpane有关闭按钮.JTabbedPane带关闭按钮;
import org.w3c.dom.Attr;
import org.w3c.dom.Node;

/**
 * 对XML元素的表示如下
 * 1、有子节点的结构（子节点点可能还有子节点的情况），都放在树形视图中
 * 2、没有子节点的结构，只有名称和值的情况可以用属性表示（用表格的方式：属性名称，属性内容，描述说明）
 * 3、元素只有文本值的直接在一个节点上显示：【元素名称】文本值
 * 4、元素文本值和子节点不能同时出现（先这样规定）
 * @author yanxi
 */
public class 主界面 extends javax.swing.JFrame { 
    树节点 复制的节点=null;
    Attr 复制的属性=null;
    JTabbedPane带关闭按钮 tabpane;
    
    /**
     * Creates new form NewJFrame
     */
    public 主界面() {
        initComponents(); 
        
        tabpane=new JTabbedPane带关闭按钮();
        this.add(tabpane, BorderLayout.CENTER);
        this.setSize(600,400);
        this.setLocationRelativeTo(null);
        
        编辑Panel p=new 编辑Panel(this);
        tabpane.add(p.file.getName(),p);
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem打开XML文件 = new javax.swing.JMenuItem();
        jMenuItem输出XML = new javax.swing.JMenuItem();
        jMenuItem退出程序 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem展开所有 = new javax.swing.JMenuItem();
        jMenuItem收起所有 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem新增节点 = new javax.swing.JMenuItem();
        jMenuItem复制节点 = new javax.swing.JMenuItem();
        jMenuItem剪切节点 = new javax.swing.JMenuItem();
        jMenuItem删除节点 = new javax.swing.JMenuItem();
        jMenu粘贴节点 = new javax.swing.JMenu();
        jMenuItem粘贴并替换当前节点 = new javax.swing.JMenuItem();
        jMenuItem粘贴为子节点 = new javax.swing.JMenuItem();
        jMenuItem粘贴为兄弟节点 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem前移一位 = new javax.swing.JMenuItem();
        jMenuItem后移一位 = new javax.swing.JMenuItem();
        jMenuItem移到头部 = new javax.swing.JMenuItem();
        jMenuItem移到尾部 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem新增属性 = new javax.swing.JMenuItem();
        jMenuItem复制属性 = new javax.swing.JMenuItem();
        jMenuItem粘贴属性 = new javax.swing.JMenuItem();
        jMenuItem剪切属性 = new javax.swing.JMenuItem();
        jMenuItem删除属性 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("文件");

        jMenuItem打开XML文件.setText("打开XML文件");
        jMenuItem打开XML文件.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem打开XML文件ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem打开XML文件);

        jMenuItem输出XML.setText("导出XML文件");
        jMenuItem输出XML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem输出XMLActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem输出XML);

        jMenuItem退出程序.setText("退出程序");
        jMenuItem退出程序.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem退出程序ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem退出程序);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("查看");

        jMenuItem展开所有.setText("展开所有");
        jMenuItem展开所有.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem展开所有ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem展开所有);

        jMenuItem收起所有.setText("收起所有");
        jMenuItem收起所有.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem收起所有ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem收起所有);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("节点");

        jMenuItem新增节点.setText("新增 ...");
        jMenuItem新增节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem新增节点ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem新增节点);

        jMenuItem复制节点.setText("复制");
        jMenuItem复制节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem复制节点ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem复制节点);

        jMenuItem剪切节点.setText("剪切");
        jMenuItem剪切节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem剪切节点ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem剪切节点);

        jMenuItem删除节点.setText("删除");
        jMenuItem删除节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem删除节点ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem删除节点);

        jMenu粘贴节点.setText("粘贴");

        jMenuItem粘贴并替换当前节点.setText("粘贴并替换当前节点");
        jMenuItem粘贴并替换当前节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem粘贴并替换当前节点ActionPerformed(evt);
            }
        });
        jMenu粘贴节点.add(jMenuItem粘贴并替换当前节点);

        jMenuItem粘贴为子节点.setText("粘贴为子节点");
        jMenuItem粘贴为子节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem粘贴为子节点ActionPerformed(evt);
            }
        });
        jMenu粘贴节点.add(jMenuItem粘贴为子节点);

        jMenuItem粘贴为兄弟节点.setText("粘贴为兄弟节点");
        jMenuItem粘贴为兄弟节点.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem粘贴为兄弟节点ActionPerformed(evt);
            }
        });
        jMenu粘贴节点.add(jMenuItem粘贴为兄弟节点);

        jMenu3.add(jMenu粘贴节点);

        jMenu6.setText("移动");

        jMenuItem前移一位.setText("前移一位");
        jMenuItem前移一位.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem前移一位ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem前移一位);

        jMenuItem后移一位.setText("后移一位");
        jMenuItem后移一位.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem后移一位ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem后移一位);

        jMenuItem移到头部.setText("移到头部");
        jMenuItem移到头部.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem移到头部ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem移到头部);

        jMenuItem移到尾部.setText("移到尾部");
        jMenuItem移到尾部.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem移到尾部ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem移到尾部);

        jMenu3.add(jMenu6);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("属性");

        jMenuItem新增属性.setText("新增");
        jMenuItem新增属性.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem新增属性ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem新增属性);

        jMenuItem复制属性.setText("复制");
        jMenuItem复制属性.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem复制属性ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem复制属性);

        jMenuItem粘贴属性.setText("粘贴");
        jMenuItem粘贴属性.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem粘贴属性ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem粘贴属性);

        jMenuItem剪切属性.setText("剪切");
        jMenuItem剪切属性.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem剪切属性ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem剪切属性);

        jMenuItem删除属性.setText("删除");
        jMenuItem删除属性.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem删除属性ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem删除属性);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem打开XML文件ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem打开XML文件ActionPerformed
        JFileChooser chooser=new JFileChooser();
        int value=chooser.showOpenDialog(this);
        if (value==chooser.APPROVE_OPTION) { 
            File file=chooser.getSelectedFile();    
            
            boolean 找到=false;
            for(int i=0;i<this.tabpane.getTabCount();i++){ 
                Component c=this.tabpane.getComponentAt(i);
                编辑Panel pane=(编辑Panel)c;
                if(pane.file.getAbsolutePath().equals(file.getAbsolutePath())){
                    找到=true;
                    tabpane.setSelectedComponent(c);
                    break;
                }
            }
            if(!找到){
                编辑Panel panel=new 编辑Panel(this, file);
                tabpane.add(file.getName(), panel);
                tabpane.setSelectedComponent(panel);
            }
        } 
    }//GEN-LAST:event_jMenuItem打开XML文件ActionPerformed

    private void jMenuItem输出XMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem输出XMLActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        DefaultMutableTreeNode root=(DefaultMutableTreeNode)pane.jTree元素.getModel().getRoot();
        树节点 xnode=(树节点)root.getUserObject();
        //System.out.println(xnode.toXMLString());
        System.out.println(pane.dom.toString());
    }//GEN-LAST:event_jMenuItem输出XMLActionPerformed

    private void jMenuItem展开所有ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem展开所有ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    展开所有子节点((DefaultMutableTreeNode) path.getLastPathComponent());
                }
            });
        }
    }//GEN-LAST:event_jMenuItem展开所有ActionPerformed

    private void jMenuItem收起所有ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem收起所有ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    收起所有子节点((DefaultMutableTreeNode) path.getLastPathComponent());
                }
            });
        }
    }//GEN-LAST:event_jMenuItem收起所有ActionPerformed

    private void jMenuItem复制节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem复制节点ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            this.复制的节点=new 树节点(xnode.n.cloneNode(true));
        }
    }//GEN-LAST:event_jMenuItem复制节点ActionPerformed

    private void jMenuItem剪切节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem剪切节点ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
            parent.remove(node);
            树节点 xnode=(树节点)node.getUserObject();this.复制的节点=xnode;
            树节点 pxnode=(树节点)parent.getUserObject();
            pxnode.n.removeChild(xnode.n); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath()));
        }
    }//GEN-LAST:event_jMenuItem剪切节点ActionPerformed

    private void jMenuItem删除节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem删除节点ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
            parent.remove(node); 
            树节点 xnode=(树节点)node.getUserObject(); 
            树节点 pxnode=(树节点)parent.getUserObject();
            pxnode.n.removeChild(xnode.n); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(parent.getPath()));
        }
    }//GEN-LAST:event_jMenuItem删除节点ActionPerformed

    private void jMenuItem粘贴并替换当前节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem粘贴并替换当前节点ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
            树节点 xnode=(树节点)node.getUserObject();
            
            //替换当前xml节点
            Node pnode=xnode.n.getParentNode();
            Node newNode=复制的节点.n.cloneNode(true);
            pnode.replaceChild(newNode, xnode.n); 
            //替换当前jtree节点
            int i=parent.getIndex(node);  
            parent.remove(i); 
            node=new 树节点(newNode).createTreeNode();
            parent.insert(node, i); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath()));
        }
    }//GEN-LAST:event_jMenuItem粘贴并替换当前节点ActionPerformed

    private void jMenuItem粘贴为子节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem粘贴为子节点ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent(); 
            Node newNode=复制的节点.n.cloneNode(true);
            
            //添加为jtree子节点
            DefaultMutableTreeNode n=new 树节点(newNode).createTreeNode();
            node.add(n);
            //添加为xml节点点
            树节点 xnode=(树节点)node.getUserObject(); 
            xnode.n.appendChild(复制的节点.n.cloneNode(true));  
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(n.getPath()));
            pane.jTree元素.scrollPathToVisible(new TreePath(n.getPath()));
        }
    }//GEN-LAST:event_jMenuItem粘贴为子节点ActionPerformed

    private void jMenuItem粘贴为兄弟节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem粘贴为兄弟节点ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
            树节点 xnode=(树节点)node.getUserObject();
            Node pnode=xnode.n.getParentNode(); 
            Node newNode=复制的节点.n.cloneNode(true);
            
            //添加为xml兄弟节点
            pnode.insertBefore(newNode, xnode.n); 
            //添加为jtree兄弟节点
            int i=parent.getIndex(node);  
            node=new 树节点(newNode).createTreeNode();
            parent.insert(node, i); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath())); 
        }
    }//GEN-LAST:event_jMenuItem粘贴为兄弟节点ActionPerformed

    private void jMenuItem前移一位ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem前移一位ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            Node 前一个=xnode.n.getPreviousSibling(); 
            if(前一个==null)return ;//已经是最前的节点了，不用移动
            
            //获取jtree中位置
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
            int i=parent.getIndex(node);  
            parent.remove(i); 
            
            //先在xml dom中完成前移一个的操作，再在jtree中完成 
            Node pnode=xnode.n.getParentNode(); 
            Node 新节点node=xnode.n.cloneNode(true);
            树节点 新节点xml=new 树节点(新节点node); 
            pnode.insertBefore(新节点node, 前一个);
            pnode.removeChild(xnode.n);
            
            //新生成jtree中节点
            parent.insert(新节点xml.createTreeNode(), i-1); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath())); 
        }
    }//GEN-LAST:event_jMenuItem前移一位ActionPerformed

    private void jMenuItem后移一位ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem后移一位ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode) path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            Node 后一个=xnode.n.getNextSibling(); 
            if(后一个==null)return ;//已经是最后的节点了，不用移动
            
            //获取jtree中位置
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent();
            int i=parent.getIndex(node);  
            parent.remove(i); 
            
            //先在xml dom中完成前移一个的操作，再在jtree中完成 
            Node pnode=xnode.n.getParentNode(); 
            Node 新节点node=xnode.n.cloneNode(true);
            树节点 新节点xml=new 树节点(新节点node); 
            Node 再后一个=后一个.getNextSibling();
            if(再后一个==null)
                pnode.appendChild(新节点node);
            else
                pnode.insertBefore(新节点node, 再后一个);
            pnode.removeChild(xnode.n);
            
            //新生成jtree中节点
            parent.insert(新节点xml.createTreeNode(), i+1); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath())); 
        }
    }//GEN-LAST:event_jMenuItem后移一位ActionPerformed

    private void jMenuItem移到头部ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem移到头部ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            Node 前一个=xnode.n.getPreviousSibling(); 
            if(前一个==null)return ;//已经是最前的节点了，不用移动
            
            //获取jtree中位置
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent(); 
            parent.remove(node); 
            
            //先在xml dom中完成前移一个的操作，再在jtree中完成 
            Node pnode=xnode.n.getParentNode(); 
            Node 新节点node=xnode.n.cloneNode(true);
            树节点 新节点xml=new 树节点(新节点node); 
            Node 头个节点=xnode.n.getFirstChild();
            pnode.insertBefore(新节点node, 头个节点);
            pnode.removeChild(xnode.n);
            
            //新生成jtree中节点
            parent.insert(新节点xml.createTreeNode(), 0); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath())); 
        }
    }//GEN-LAST:event_jMenuItem移到头部ActionPerformed

    private void jMenuItem移到尾部ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem移到尾部ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            Node 后一个=xnode.n.getNextSibling(); 
            if(后一个==null)return ;//已经是最后的节点了，不用移动
            
            //获取jtree中位置
            DefaultMutableTreeNode parent=(DefaultMutableTreeNode)node.getParent(); 
            parent.remove(node); 
            
            //先在xml dom中完成前移一个的操作，再在jtree中完成 
            Node pnode=xnode.n.getParentNode(); 
            Node 新节点node=xnode.n.cloneNode(true);
            树节点 新节点xml=new 树节点(新节点node); 
            pnode.appendChild(新节点node);
            pnode.removeChild(xnode.n);
            
            //新生成jtree中节点
            parent.add(新节点xml.createTreeNode()); 
            
            pane.jTree元素.updateUI();
            pane.jTree元素.setSelectionPath(new TreePath(node.getPath())); 
        }
    }//GEN-LAST:event_jMenuItem移到尾部ActionPerformed

    private void jMenuItem新增节点ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem新增节点ActionPerformed
        new 对话框(this, true);
    }//GEN-LAST:event_jMenuItem新增节点ActionPerformed

    private void jMenuItem新增属性ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem新增属性ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            Attr attr=pane.dom.createAttribute("新属性");
            xnode.n.getAttributes().setNamedItem(attr); 
            if(xnode.n.getAttributes().getNamedItem("新属性")!=null)
                System.out.println("新属性 已经存在");
            pane.setAttributeTable(xnode.n);
            pane.jTree元素.updateUI();
        }
    }//GEN-LAST:event_jMenuItem新增属性ActionPerformed

    private void jMenuItem复制属性ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem复制属性ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            if(pane.jTable属性列表.getSelectedRow()<0)return;
            this.复制的属性=(Attr)xnode.n.getAttributes().
                    item(pane.jTable属性列表.getSelectedRow()).cloneNode(true); 
        }
    }//GEN-LAST:event_jMenuItem复制属性ActionPerformed

    private void jMenuItem粘贴属性ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem粘贴属性ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            if(pane.jTable属性列表.getSelectedRow()<0)return;
            if(this.复制的属性==null)return ;
            xnode.n.getAttributes().setNamedItem(this.复制的属性.cloneNode(true)); 
            pane.setAttributeTable(xnode.n);
            pane.jTree元素.updateUI();
        }
    }//GEN-LAST:event_jMenuItem粘贴属性ActionPerformed

    private void jMenuItem剪切属性ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem剪切属性ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            if(pane.jTable属性列表.getSelectedRow()<0)return;
            this.复制的属性=(Attr)xnode.n.getAttributes().item(pane.jTable属性列表.getSelectedRow()).cloneNode(true); 
            xnode.n.getAttributes().removeNamedItem(this.复制的属性.getName());
            pane.setAttributeTable(xnode.n);
            pane.jTree元素.updateUI();
        }
    }//GEN-LAST:event_jMenuItem剪切属性ActionPerformed

    private void jMenuItem删除属性ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem删除属性ActionPerformed
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent(); 
        TreePath path = pane.jTree元素.getSelectionPath();
        if (path != null) {
            DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent();
            树节点 xnode=(树节点)node.getUserObject();
            if(pane.jTable属性列表.getSelectedRow()<0)return;
            Node a=xnode.n.getAttributes().item(pane.jTable属性列表.getSelectedRow()); 
            xnode.n.getAttributes().removeNamedItem(a.getNodeName());
            pane.setAttributeTable(xnode.n);
            pane.jTree元素.updateUI();
        }
    }//GEN-LAST:event_jMenuItem删除属性ActionPerformed

    private void jMenuItem退出程序ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem退出程序ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem退出程序ActionPerformed
    

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(主界面.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(主界面.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(主界面.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(主界面.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() { 
                new 主界面().setVisible(true);
            }
        });
    } 
    private void 展开所有子节点(DefaultMutableTreeNode node) {
        if (node.isLeaf()) return; 
        for (int i = 0; i < node.getChildCount(); i++) 
            展开所有子节点((DefaultMutableTreeNode) node.getChildAt(i)); 
        
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent();  
        pane.jTree元素.expandPath(new TreePath(node.getPath())); 
    }
    private void 收起所有子节点(DefaultMutableTreeNode node) {
        if (node.isLeaf()) return; 
        for (int i = 0; i < node.getChildCount(); i++) 
            收起所有子节点((DefaultMutableTreeNode) node.getChildAt(i)); 
        编辑Panel pane=(编辑Panel)this.tabpane.getSelectedComponent();  
        pane.jTree元素.collapsePath(new TreePath(node.getPath()));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem删除属性;
    public javax.swing.JMenuItem jMenuItem删除节点;
    public javax.swing.JMenuItem jMenuItem前移一位;
    private javax.swing.JMenuItem jMenuItem剪切属性;
    public javax.swing.JMenuItem jMenuItem剪切节点;
    private javax.swing.JMenuItem jMenuItem后移一位;
    private javax.swing.JMenuItem jMenuItem复制属性;
    public javax.swing.JMenuItem jMenuItem复制节点;
    private javax.swing.JMenuItem jMenuItem展开所有;
    private javax.swing.JMenuItem jMenuItem打开XML文件;
    private javax.swing.JMenuItem jMenuItem收起所有;
    private javax.swing.JMenuItem jMenuItem新增属性;
    public javax.swing.JMenuItem jMenuItem新增节点;
    private javax.swing.JMenuItem jMenuItem移到头部;
    private javax.swing.JMenuItem jMenuItem移到尾部;
    private javax.swing.JMenuItem jMenuItem粘贴为兄弟节点;
    private javax.swing.JMenuItem jMenuItem粘贴为子节点;
    private javax.swing.JMenuItem jMenuItem粘贴属性;
    private javax.swing.JMenuItem jMenuItem粘贴并替换当前节点;
    private javax.swing.JMenuItem jMenuItem输出XML;
    private javax.swing.JMenuItem jMenuItem退出程序;
    public javax.swing.JMenu jMenu粘贴节点;
    // End of variables declaration//GEN-END:variables
}
